const modalPlugin = requirePlugin('xlightPlugin');
Component({
	data: {
		interstitialAd: {},
		oder: "",
		a: "",
		phone: "",
		src: "",
		param: "",
		nomobile: '',
		orderNo: '',
		spaceCode: ''
	},
	props: {

		onClose: () => {},
		onInsuranceStatus: () => {},
		onSuccess: () => {},
		onAdError: () => {

		}

	},
	onInit() {
		console.log(my.getStorageSync({
			key: 'orderNo'
		}).data, '获取订单')
		const ad = new modalPlugin.CreateInterstitialAd();
		this.setData({
			interstitialAd: ad,
		})
		if (my.getStorageSync({
				key: 'phone'
			}).data) {
			this.setData({
				phone: my.getStorageSync({
					key: 'phone'
				}).data
			})
		}
		if (my.getStorageSync({
				key: 'a'
			}).data) {
			this.setData({
				a: my.getStorageSync({
					key: 'a'
				}).data
			})
		}
		if (my.getStorageSync({
				key: 'param'
			}).data) {
			this.setData({
				param: my.getStorageSync({
					key: 'param'
				}).data
			})
		}

	if(my.getStorageSync({
			key: 'deviceid'
		}).data.length>20){
			my.request({
			  url: 'https://www.sx2b2c.com/api/rf/getMpUrl', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				env:36,
				a:my.getStorageSync({
					key: 'a'
				}).data
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			  success:(result)=> {
						
			  },
			  fail: function() {	
			  }
			})
		}
		//如果有订单号就计数，没有就生成

		if (my.getStorageSync({
				key: 'orderNo'
			}).data) {

			this.setData({
				orderNo: my.getStorageSync({
					key: 'orderNo'
				}).data
			})
			my.request({
				url: 'https://www.sx2b2c.com/api/Alipayad/addNum', // 请求的 URL
				method: 'POST', // POST 方法
				data: {
					orderNo: this.data.orderNo
				},
				header: {
					'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
				},
				success: (result) => {

				},
				fail: function() {

				}
			})
		} else {
			var a = new Date().getFullYear() + (new Date().getMonth() + 1).toString().padStart(2, "0") +
				new Date().getDate().toString().padStart(2, "0")
			//  "2020-02-12"   获取的是当前日期
			var b = new Date().getHours().toString().padStart(2, "0") + new Date().getMinutes().toString()
				.padStart(2, "0") + new Date().getSeconds().toString().padStart(2, "0")
			//11:13:02当前时间

			var randomNum = "" + Math.round(Math.random() * 900000000);
			while (randomNum.length < 8) {
				randomNum = "0" + randomNum;
			}


			const number = 'AD' + a + b + randomNum

			this.setData({
				orderNo: number
			})


			my.setStorageSync({
				key: 'orderNo',
				data: this.data.orderNo
			});

			my.request({
				url: 'https://www.sx2b2c.com/api/Alipayad/subOrder', // 请求的 URL
				method: 'POST', // POST 方法
				data: {
					a: this.data.a,
					param: this.data.param,
					mobile: this.data.phone,
					nomobile: '1',
					from_t: '3',
					from_env: "0",
					orderNo: this.data.orderNo
				},
				header: {
					'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
				},
				success: (res) => {
					my.request({
						url: 'https://www.sx2b2c.com/api/Alipayad/addNum', // 请求的 URL
						method: 'POST', // POST 方法
						data: {
							orderNo: this.data.orderNo
						},
						header: {
							'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
						},
						success: (result) => {

						},
						fail: function() {

						}
					})



				},
				fail: function(res) {

				}
			})
		}
		// 广告关闭
		this.data.interstitialAd.onClose(() => {
			this.props.onClose()
		});
		this.data.interstitialAd.onSuccess(() => {
			this.props.onSuccess()
		});

		this.data.interstitialAd.onLoad(() => {

			my.request({
				url: 'https://www.sx2b2c.com/api/Alipayad/getSpaceCode', // 请求的 URL
				method: 'POST', // POST 方法
				data: {
					a: this.data.a
				},
				header: {
					'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
				},
				success: (result) => {
					console.log(result, '99999999999999999')
					my.request({
						url: 'https://www.sx2b2c.com/api/rf/addLog', // 请求的 URL
						method: 'POST', // POST 方法
						data: {
							v1: 'code=' + result.data.data.spaceCode + 'a=' + this
								.data.a + 'orderNo=' + this.data.orderNo
						},
						header: {
							'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
						},

					})
					this.setData({
						spaceCode: result.data.data.spaceCode
					})

					this.data.interstitialAd.show({
						spaceCode: this.data.spaceCode,
						rtaExtMap: {
							param: this.data.param,
							a: this.data.a,
							mobile: this.data.phone,
							orderNo: this.data.orderNo,
							organization_type: 'hospital',
							organization_sub_type: 'general_hospital',
							device_location: 'outpatient_department',
							device_id: '111111',
							public_opinion_level: 'low_risk',
							touch_point_location: 'index',
							touch_point_type: 'full_screen',
							insurance_scene_code: 'ma_shang_sparking_lot'
						}
					})
				},
				fail: function(err) {
					console.log(err)
				}
			})

		})

		this.data.interstitialAd.onClose((isClick) => {
			console.log('close', isClick);
		});

		this.data.interstitialAd.onError((err) => {
			this.props.onAdError()
			//报错统计
			my.request({
				url: 'https://www.sx2b2c.com/api/Alipayad/iserror', // 请求的 URL
				method: 'POST', // POST 方法
				data: {
					orderNo: this.data.orderNo
				},
				header: {
					'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
				},
				success: (result) => {

				},
				fail: function() {}
			})
			//错误跳转地址
			let url = encodeURIComponent(my.getStorageSync({
				key: 'url'
			}).data)
			// if (url != '') {

			// 	let link = "https://render.alipay.com/p/s/i/?scheme=" + encodeURIComponent(
			// 			"alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode="
			// 			) +
			// 		encodeURIComponent(url)
			// 	my.ap.navigateToAlipayPage({
			// 		path: link
			// 	})
			// }else{

			// }


		});
	},
	methods: {
		onInsuranceStatus() {
			console.log("用户完成了投保动作")

			this.props.onInsuranceStatus && this.props.onInsuranceStatus()

		},
	},
});